﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.QuanHuyenService;
namespace eTMS.Controllers{
public class QuanHuyenControllerBase : CommonController{
	public QuanHuyenControllerBase()
		: base()
	{	
		this.dao = new QuanHuyenServiceClient();
	}
	~QuanHuyenControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private QuanHuyenEntity oQuanHuyen;	
	public QuanHuyenEntity QuanHuyenObject{
		get{
			return oQuanHuyen;
		}
		set{
			oQuanHuyen=value;
		}
	}
	protected QuanHuyenServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSQuanHuyen(false);			
			return _Table;
		}
	}
	public void LoadAllDSQuanHuyen(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oQuanHuyen==null)return false;
		bool result=true;
		try{
			oQuanHuyen = dao.SaveRet(oQuanHuyen);
		}
		catch{
			result=false;
		}
		if(result){
			_ListQuanHuyen.Add(oQuanHuyen);
		}
		return result;
	}
	public bool Update(){
		if(oQuanHuyen==null)return false;
		return dao.Update(oQuanHuyen);
	}
	public bool Delete(){
		if(oQuanHuyen==null)return false;
		bool retvalue = dao.Delete(oQuanHuyen);
		if(retvalue){			
			_ListQuanHuyen.Remove(oQuanHuyen);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 QuanHuyen_ID){
		bool retvalue = dao.DeleteByID(QuanHuyen_ID);		
		if(retvalue){
			QuanHuyenEntity oDeleted=FindQuanHuyen(QuanHuyen_ID);
			if(oDeleted!=null){
				_ListQuanHuyen.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oQuanHuyen = new QuanHuyenEntity();
	}
	//Check if exists MaQuanHuyen
	public bool CheckExistsMaQuanHuyen(object value){	
		return dao.SelectOneByMaQuanHuyen(value.ToString()).MaQuanHuyen!=null;
    }    
    //Copy data from data row in grid to object
    public QuanHuyenEntity MapDataRowToObject(DataRow row){
		QuanHuyenEntity o = new QuanHuyenEntity();
		 o.MaQuanHuyen=Convert.ToString(row["MaQuanHuyen"]);
		 o.TenQuanHuyen=Convert.ToString(row["TenQuanHuyen"]);
		 o.GhiChu=Convert.ToString(row["GhiChu"]);
		 if(!row.IsNull("TinhThanh_ID"))
			o.TinhThanh_ID=(Int32)(row["TinhThanh_ID"]);
		 if(!row.IsNull("QuanHuyen_ID"))
		    o.QuanHuyen_ID=(Int32)(row["QuanHuyen_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(QuanHuyenEntity o,DataRow row){
		row["MaQuanHuyen"]=o.MaQuanHuyen;
		row["TenQuanHuyen"]=o.TenQuanHuyen;
		row["GhiChu"]=o.GhiChu;
		row["TinhThanh_ID"]=o.TinhThanh_ID;
		row["QuanHuyen_ID"]=o.QuanHuyen_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.QuanHuyenObject,row);
    }
	public void LoadObject(Int32 QuanHuyen_ID){
		QuanHuyenEntity oLoad=FindQuanHuyen(QuanHuyen_ID);
		if(oLoad==null){
			oQuanHuyen=this.LoadQuanHuyen(QuanHuyen_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oQuanHuyen.IsNullQuanHuyen_ID;
			if(!bNotIsNull){
				this.ListQuanHuyen.Add(oQuanHuyen);
			}
		}
		else{
			oQuanHuyen=oLoad;
		}
	}
	#region CachedList QuanHuyen
	private static List<QuanHuyenEntity> _ListQuanHuyen=new List<QuanHuyenEntity>();
    public List<QuanHuyenEntity>ListQuanHuyen{
		get{
			return _ListQuanHuyen;
		}
	}
	public static List<QuanHuyenEntity>ListQuanHuyenStatic{
		get{
			return _ListQuanHuyen;
		}
	}
	public QuanHuyenEntity FindQuanHuyen(Int32 QuanHuyen_ID){
		QuanHuyenEntity oResult=this.ListQuanHuyen.Find(delegate(QuanHuyenEntity oFind){
			bool result=true; 
			result&=oFind.QuanHuyen_ID.Equals(QuanHuyen_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllQuanHuyen(bool clearCache){
		if(_ListQuanHuyen.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		QuanHuyenServiceClient dao = new QuanHuyenServiceClient();
		_ListQuanHuyen=new List<QuanHuyenEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public QuanHuyenEntity LoadQuanHuyen(Int32 QuanHuyen_ID){			
		QuanHuyenEntity o=dao.SelectOne(QuanHuyen_ID);		
		return o;
	}
	#endregion
}
}
